<?php
class Model_Friend extends Df_Model{
	

	private $user_id;
	public function __construct($id = null) {
		$this->user_id = $id;
		parent::__construct(new Model_DbTable_UsersFriends(), $id);
	}
	
	//return array with friends!
	public function getFriendsList($user_id) {
		return $this->_dbTable->fetchAll(
			$this->_dbTable->select()
				->where('user_id = ?',$user_id)
				->orWhere('user_friend = ?',$user_id)
			);
	}
	
	public function checkIfFriends($user_id, $friend_id) {
		$friend = $this->_dbTable->fetchAll(
			$this->_dbTable->select()
				->where('user_id = ?',$user_id)
				->where('user_friend = ?',$friend_id)
		)->count();
		
		if($friend)return true;
		
		$friend = $this->_dbTable->fetchAll(
			$this->_dbTable->select()
				->where('user_id = ?',$friend_id)
				->where('user_friend = ?',$user_id)
		)->count();
		if($friend)return true;
		return false;
		
	}
	
	
	public function removeFriends($user_id,$friend_id) {
		$row = $this->_dbTable->fetchRow(
			$this->_dbTable->select()
				->where('user_id = ?',$user_id)
				->where('user_friend = ?',$friend_id)
		);
		
		//do it, because user can be in user_id or in user_friend position
		if(!$row){
			$row = $this->_dbTable->fetchRow(
			$this->_dbTable->select()
				->where('user_id = ?',$friend_id)
				->where('user_friend = ?',$user_id)
			);
		}
		
		$this->_dbTable->find($row->id)->current()->delete();
	}
	
}